package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ChargingPilesAnalysis;
import com.ruoyi.system.domain.TbChargingpilesstation;
import com.ruoyi.system.mapper.TbChargingpilesstationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbChargingpilesMapper;
import com.ruoyi.system.domain.TbChargingpiles;
import com.ruoyi.system.service.ITbChargingpilesService;

/**
 * 充电桩状态监控Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class TbChargingpilesServiceImpl implements ITbChargingpilesService
{
    @Autowired
    private TbChargingpilesMapper tbChargingpilesMapper;
    @Autowired
    private TbChargingpilesstationMapper tbChargingpilesstationMapper;

    /**
     * 查询充电桩状态监控
     *
     * @param cid 充电桩状态监控主键
     * @return 充电桩状态监控
     */
    @Override
    public TbChargingpiles selectTbChargingpilesByCid(Long cid)
    {
        return tbChargingpilesMapper.selectTbChargingpilesByCid(cid);
    }

    /**
     * 查询充电桩状态监控列表
     *
     * @param tbChargingpiles 充电桩状态监控
     * @return 充电桩状态监控
     */
    @Override
    public List<TbChargingpiles> selectTbChargingpilesList(TbChargingpiles tbChargingpiles)
    {
        Long uid = SecurityUtils.getLoginUser().getUserId();
        List<Long> sids = tbChargingpilesstationMapper.getsidByUid(uid);
        List<TbChargingpiles> Piles = tbChargingpilesMapper.selectTbChargingpilesList(tbChargingpiles);

        if (!SecurityUtils.isAdmin(uid)) {
            List<TbChargingpiles> list = Piles.stream().filter(station -> sids.contains(station.getSid().intValue())).collect(Collectors.toList());
            return list;
        }
        return Piles;
    }

    /**
     * 新增充电桩状态监控
     *
     * @param tbChargingpiles 充电桩状态监控
     * @return 结果
     */
    @Override
    public int insertTbChargingpiles(TbChargingpiles tbChargingpiles)
    {
        return tbChargingpilesMapper.insertTbChargingpiles(tbChargingpiles);
    }

    /**
     * 修改充电桩状态监控
     *
     * @param tbChargingpiles 充电桩状态监控
     * @return 结果
     */
    @Override
    public int updateTbChargingpiles(TbChargingpiles tbChargingpiles)
    {
        return tbChargingpilesMapper.updateTbChargingpiles(tbChargingpiles);
    }

    /**
     * 批量删除充电桩状态监控
     *
     * @param cids 需要删除的充电桩状态监控主键
     * @return 结果
     */
    @Override
    public int deleteTbChargingpilesByCids(Long[] cids)
    {
        return tbChargingpilesMapper.deleteTbChargingpilesByCids(cids);
    }

    /**
     * 删除充电桩状态监控信息
     *
     * @param cid 充电桩状态监控主键
     * @return 结果
     */
    @Override
    public int deleteTbChargingpilesByCid(Long cid)
    {
        return tbChargingpilesMapper.deleteTbChargingpilesByCid(cid);
    }

    @Override
    public ChargingPilesAnalysis getChargingPileAnalysis() {
        return tbChargingpilesMapper.analyse();
    }
}
